# -*- coding:utf-8 -*-

#导入相应的模块
import pygame
from pygame.locals import *
import time,random

class HeroPlane:
    """玩家飞机(hero)"""
    def __init__(self,screen):
        self.x = 200
        self.y = 400
        self.screen = screen
        self.image = pygame.image.load("./images/me.png")
        self.bullet_list = [] #用于存放玩家子弹的列表

    def display(self,enemy):
        """绘制子弹"""
        for b in self.bullet_list:
            b.display()
            if b.move():
                self.bullet_list.remove(b)
        """绘制玩家飞机"""
        self.screen.blit(self.image,(self.x,self.y))
        if self.beHit(enemy):
            return True

    def move_left(self):
        """左移动飞机"""
        self.x -= 5
        if self.x <= 0:
            self.x = 0

    def move_right(self):
        """右移动飞机"""
        self.x += 5
        if self.x >= 406:
            self.x = 406

    def move_up(self):
        """上移动飞机"""
        self.y -= 5
        if self.y <= 0:
            self.y = 0

    def move_down(self):
        """下移动飞机"""
        self.y += 5
        if self.y >= 492:
            self.y = 492

    def fire(self):
        """发射子弹方法"""
        self.bullet_list.append(Bullet(self.screen,self.x,self.y,1))
        print(len(self.bullet_list))

    def beHit(self,Enemy):
        #遍历所有子弹，并执行碰撞检测
        for bo in Enemy.bullet_Elist:
            if bo.x > self.x+12 and bo.x < self.x+92 and bo.y > self.y+20 and bo.y < self.y+60:
                Enemy.bullet_Elist.remove(bo)
                for temp in range(4):
                    bombImage = pygame.image.load("./images/bomb/bomb_" + str(temp) + ".png")
                    self.screen.blit(bombImage, (self.x+37, self.y+22))
                    time.sleep(0.04)
                print("game over")
                return True

class Bullet:
    """子弹类"""
    def __init__(self,screen_temp,x,y,bulletType):
        self.x = x + 53
        self.y = y
        self.screen = screen_temp
        self.image = pygame.image.load("./images/pd.png")
        self.bulletType = bulletType

    def display(self):
        """绘制子弹"""
        self.screen.blit(self.image,(self.x,self.y))

    def move(self):
        """子弹移动"""
        if self.bulletType:
            self.y -= 10
            if self.y <= -20:
                return True
        else:
            self.y += 5
            if self.y >= 588:
                return True

class EnemyPlane:
    """敌机类"""
    _planeType = ""
    def __init__(self,screen_temp):
        self.x = random.choice(range(408))
        self.y = -75
        self._planeType = str(random.choice(range(3)))
        self.screen = screen_temp
        self.image = pygame.image.load("./images/e"+self._planeType+".png")
        self.bullet_Elist = []  # 用于存放敌机子弹的列表

    def display(self):
        """绘制子弹"""
        for b in self.bullet_Elist:
            b.display()
            if b.move():
                self.bullet_Elist.remove(b)

        """绘制敌机"""
        self.screen.blit(self.image,(self.x,self.y))

    def move(self,hero):
        """敌机移动"""
        self.y += 3
        if random.choice(range(10000)) > 9800:
            self.fire()
        if self.y >= 568:
            return True

        #遍历所有子弹，并执行碰撞检测
        for bo in hero.bullet_list:
            if bo.x > self.x+12 and bo.x < self.x+92 and bo.y > self.y+20 and bo.y < self.y+60:
                hero.bullet_list.remove(bo)
                for temp in range(4):
                    bombImage = pygame.image.load("./images/bomb/bomb_" + str(temp) + ".png")
                    if self._planeType == "0":
                        self.screen.blit(bombImage, (self.x+42, self.y+25))
                    elif self._planeType == "1":
                        self.screen.blit(bombImage, (self.x+30, self.y+22))
                    else:
                        self.screen.blit(bombImage, (self.x+36, self.y+21))
                    time.sleep(0.04)
                return True

    def fire(self):
        """发射子弹方法"""
        self.bullet_Elist.append(Bullet(self.screen,self.x,self.y,0))
        print(len(self.bullet_Elist))

def key_control(hero_temp,enemy_temp):
    """键盘控制函数"""
    #执行退出操作
    for event in pygame.event.get():
        if event.type == QUIT:
            print("exit()")
            exit()

    #获取按键信息
    pressed_keys = pygame.key.get_pressed()

    #做判断，并执行对象的操作
    if pressed_keys[K_LEFT] or pressed_keys[K_a]:
        print("Left")
        hero_temp.move_left()
    elif pressed_keys[K_RIGHT] or pressed_keys[K_d]:
        print("Right")
        hero_temp.move_right()

    if pressed_keys[K_UP] or pressed_keys[K_w]:
        print("Up")
        hero_temp.move_up()
    elif pressed_keys[K_DOWN] or pressed_keys[K_s]:
        print("Down")
        hero_temp.move_down()

    if pressed_keys[K_SPACE]:
        print("Space")
        hero_temp.fire()
    #做判断，并执行对象的操作

def main():
    """主程序函数"""
    #创建游戏窗口
    screen = pygame.display.set_mode((512,568),0,0)

    #创建一个游戏背景
    background = pygame.image.load("./images/bg2.jpg")

    #创建玩家飞机
    hero = HeroPlane(screen)

    m = -968
    enemylist = []#存放敌机的列表
    while True:
        #绘制画面
        screen.blit(background,(0,m))
        m += 2
        if m >= -200:
            m = -968

        #绘制玩家飞机
        # hero.display()

        #执行键盘控制
        # key_control(hero)

        #随机绘制敌机
        if random.choice(range(15)) == 10:
            enemylist.append(EnemyPlane(screen))
            print(len(enemylist))

        #遍历敌机并绘制移动
        for em in enemylist:
            em.display()
            if hero.display(em):
                gameover = pygame.image.load("./images/gameover.jpg")
                for temp in range(40):
                    self.screen.blit(gameover, (155, 200))
                    time.sleep(0.04)
                break
            key_control(hero,em)
            if em.move(hero):
                enemylist.remove(em)

        #更新显示
        pygame.display.update()

        #定时显示，24帧每秒就可以实现画面连续
        time.sleep(0.04)

#判断当前是否是主运行程序，并调用
if __name__ == "__main__":
    main()
